package com.indexingsystem.db.redis;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.ObjectUtils;

import com.indexingsystem.boss.entity.Institutions;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.db.redis.impl.RedisManager;


/**    
 * <p>Title: MerchantRedis.java</p>    
 * <p>Description: 描述 </p>   
 * @author vania      
 * @version 1.0    
 * @created 2015年3月20日 上午10:11:55   
 */

public class InstitutionsRedis {
	
	private static RedisService redis = new RedisManager();
	
	
	
	/**
	 * 删除机构key缓存<key:digitalhome_institutions_institutions_id> 多个批量
	 * @param QueryInstitutionsVo
	 * @return 是否删除成功
	 */
	public static boolean del_digitalhome_institutions_institutions_id(Institutions... institutiones) {
		for (Institutions institutions : institutiones) {
			String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions.getInstitutionId()) ;
			
			redis.del(key);
		}
		return true;
	}
	
	
	/**
	 * 
	 * @Title: del_digitalhome_institutions_institutions_id  单个删除
	 * @see: TODO
	 * @param institutions_id
	 * @return
	 * @return Boolean    返回类型 
	 * @throws
	 */
	public static boolean del_digitalhome_institutions_institutions_id(String institutions_id) {
		String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions_id);
		Long result = redis.del(key);
		return result > 0;
	}
	
	
	
	/**
	 * 删除通过机构id 和 其它属性组合过滤删除.
	 * 
	 * @Title: del_digitalhome_institutions_institutions_id 
	 * @see: TODO
	 * @param institutions_id
	 * @param fields
	 * @return
	 * @return Boolean    返回类型 
	 * @throws
	 */
	public static boolean del_digitalhome_institutions_institutions_id(String institutions_id, String... fields) {
		String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions_id);
		Long result = redis.hdel(key, fields);
		return result > -1;
	}
	
	
	
	/**
	 * 获取单个fields值
	 * 
	 * @Description: 使用Redis的哈希结构存储HSET key field value
	 * key为digitalhome_institutions_institutions_id
	 * field包含 parentId/institutionName/city/contactName/等，可以通过POJO类属性来查询
	 * @param institutions_id 机构id
	 * @param fields 要取的field的某一名称
	 * @return String value值
	 * @throws
	 */
	public static String get_digitalhome_institutions_institutions_id(String institutions_id, String fields) {
		String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions_id) ;
		return redis.getMapValue(key, fields);
	}
	
	
	
	/**
	 * 获取所有fields值
	 * 
	 * @Description: 使用Redis的哈希结构存储HSET key field value
	 * key为digitalhome_institutions_institutions_id
	 * field包含 POJO
	 * @param institutions_id 机构id
	 * @return Map<String,String>
	 * @throws
	 */
	public static Map<String,String> getAll_digitalhome_institutions_institutions_id(String institutions_id) {
		String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions_id) ;
		return redis.getMap(key);
	}
	
	
	/**
	 * 
	 * @Title: get_digitalhome_institutions_institutions_id
	 * @see: TODO
	 * @param institutions_id
	 * @return
	 * @return String   返回类型 
	 * @throws
	 */
	public static String get_digitalhome_institutions_institutions_id(String institutions_id) {
		String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions_id) ;
		if(redis.exists(key)){
			return redis.get(key);
		}else{
			return null;
		}
	}
	
	/**
	 * 
	 * @Title: getSetAll_digitalhome_institutions_institutions_id 
	 * @param institutions_id
	 * @return
	 * @return Set<String>    返回类型 
	 * @throws
	 */
	public static Set<String> getSetAll_digitalhome_institutions_institutions_id(String institutions_id) {
		String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions_id);
		Set<String> result = redis.getSet(key);
		return result;
	}
	
	
	
	
	/**
	 * 设置所有fields的value值
	 * 
	 * @Description: 使用Redis的哈希结构存储HSET key field value
	 * key为digitalhome_institutions_institutions_id
	 * field包含 name/point_partner_no/openapi_partner_no/appkey/appsecret/order_display/return_url/bank_name/qr_logo/bank_type
	 * @param QueryInstitutionsVo 机构对象
	 * @return boolean是否设置成功
	 * @throws
	 */
	public static Boolean set_digitalhome_institutions_institutions_id(Institutions... institutiones) {
		for (Institutions institutions : institutiones) {
			/* 缓存该机构下机构信息 */
			String key = RedisKeyUtil.digitalhome_institutions_institutions_id(institutions.getInstitutionId()) ;

			Map<String, String> hash = new HashMap<String, String>();
			//缓存机构id
			hash.put("institutionId", institutions.getInstitutionId());
			
			//上级机构id
			hash.put("parentId", institutions.getParentId());
			
			//机构排序
			hash.put("institutionOrder", ObjectUtils.toString(institutions.getInstitutionOrder(), ""));
			
            //公司名称
			hash.put("institutionName", ObjectUtils.toString(institutions.getInstitutionName(), ""));

            //显示名称
			hash.put("institutionDisplayName", institutions.getInstitutionDisplayName());

			//机构类型
			hash.put("institutionType", ObjectUtils.toString(institutions.getInstitutionType(), ""));
			
			

//    private String nature;
//
//    private String industry;
//
//    private String province;
//
//    private String city;
//
//    private String address;
//
//    private String phone;
//
//    private String contactName;
//
//    private String postcode;
//
//    private String description;
//
//    private Integer enabled;
//
//    private String validDate;
//
//    private Date createDate;
//
//    private String createUserId;
//
//    private Date lastUpdatedDate;
//
//    private String lastUpdatedUserId;
			redis.putMap(key, hash);
		}
		return true;
	}
	
	
	/**
	 * 
	 * @Title: set_digitalhome_user_institutions_all_institutions_id_user_id 
	 * @see: TODO
	 * @param user_id
	 * @param institutions_id
	 * @param user_institution_id
	 * @return
	 * @return Boolean    返回类型 
	 * @throws
	 */
	public static boolean set_digitalhome_user_institutions_all_institutions_id_user_id(String user_id,String institutions_id,String user_institution_id) {
		String key = RedisKeyUtil.digitalhome_user_institutions_all_institutions_id_user_id( user_id, institutions_id);
		Boolean  result = redis.put(key, ObjectUtils.toString(user_institution_id, "")) > -1;
		LogCvt.info("设置缓存:" + key + "成功!");
		return result ;
	}
	
	
	/**
	 * 如果有多个user_institution_id 值，同时删除多个.
	 * @param user_id
	 * @param institutions_id
	 * @param user_institution_id
	 * @return
	 */
	public static boolean del_digitalhome_user_institutions_all_institutions_id_user_id(String user_id, String institutions_id, String user_institution_id) {
		String key = RedisKeyUtil.digitalhome_user_institutions_all_institutions_id_user_id(user_id, institutions_id);
		Long result = redis.srem(key, ObjectUtils.toString(user_institution_id, ""));
		return result > -1;
	}
}